import IntroScene.IntroPanel;
import IntroScene.PicPanel;
import IntroScene2.IntroPanel2;
import Player.MusicPlayer;
import Scene1.ChoicePanel;
import Scene1.GamePanel;
import Scene2.PlantPanel;
import Scene4.AnimalPanel;
import Scene4.AnimalPanel2;
import Scene4.AnimalPanel3;
import Scene5.SnowPanel;
import Scene5.SpeakPanel;
import Scene6.SpeakPanel2;
import Scene6.TreePanel;
import SceneBegin.BeginPanel;
import SceneFinal.FinalPanel;
import Scnen3.J;
import Scnen3.PlantPanel2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameFrame extends JFrame {
    int flagScene1 = 0;
    int clickCount = 0;
    int clickCount2 = 0;
    int clickCount3 = 0;
    int clickCount4 = 0;
    int clickCount5 = 0;

   public JPanel cards = new JPanel(new CardLayout());
    GamePanel panel = new GamePanel();
    SnowPanel snowPanel;
    IntroPanel2 introPanel2 = new IntroPanel2();
    JButton win = new JButton();
    AnimalPanel animalPanel = new AnimalPanel(win);


    BeginPanel beginPanel = new BeginPanel();
    IntroPanel introPanel = new IntroPanel();
    int clickCount6 = 0;
    MusicPlayer button = new MusicPlayer("/music/button.mp3");


    public GameFrame() {

        GameFrame x = this;
        JButton haoye2 = new JButton();
        JButton buttonAg = new JButton();
        TreePanel treePanel = new TreePanel(haoye2,buttonAg);
        //TODO 720 1136
        this.setSize(720,1136);
        PlantPanel2 plantPanel2 = new PlantPanel2(this);


        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(cards, BorderLayout.CENTER);


        PlantPanel plantPanel = new PlantPanel();
        SpeakPanel2 speakPanel2 = new SpeakPanel2(this);

        AnimalPanel2 animalPanel2 = new AnimalPanel2();
        ChoicePanel choicePanel = new ChoicePanel(plantPanel,animalPanel2);
        AnimalPanel3 animalPanel3 = new AnimalPanel3();
        SpeakPanel speakPanel = new SpeakPanel();
        JButton haoye = new JButton();
        FinalPanel fp = new FinalPanel();
        snowPanel= new SnowPanel(this,haoye);



        cards.add("sceneBegin",beginPanel);
        cards.add("sceneIntro",introPanel);
        cards.add("sp2",speakPanel2);
        cards.add("scene1", panel);
        cards.add("scene2", plantPanel);
        cards.add("scene3",plantPanel2);
        cards.add("animal",animalPanel);

        cards.add("scene6",treePanel);
        cards.add("ap2",animalPanel2);
        cards.add("ap3",animalPanel3 );
        cards.add("choiceScene",choicePanel);
        cards.add("i2",introPanel2);
        cards.add("sp",speakPanel);
        cards.add("scene5",snowPanel);
        cards.add("f",fp);
        CardLayout cl = (CardLayout) cards.getLayout();
        cl.show(cards, "sceneBegin");



        JButton buttonNextf = new JButton();

        fp.setLayout(null);
        fp.add(buttonNextf);
        buttonNextf.setContentAreaFilled(false);
        buttonNextf.setBorder(null);
        buttonNextf.addActionListener(e -> {
            fp.changeFont();
        });
        buttonNextf.setBounds(1200,970,30,30);
        buttonNextf.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        haoye.setContentAreaFilled(false);
        haoye.setBorder(null);
        haoye.setBounds(880,970,225,97);
        haoye.addActionListener((e)->{
            cl.show(cards,"sp");
            speakPanel.changePic();
            speakPanel.reSize(this);
            speakPanel.requestFocus();
        });



        //sp2
        JButton buttonNext6 = new JButton();
        JButton tbuttonBegin = new JButton();
        speakPanel2.setLayout(null);
        speakPanel2.add(buttonNext6);
        buttonNext6.setContentAreaFilled(false);
        buttonNext6.setBorder(null);
        buttonNext6.addActionListener(e -> {
            button.play();
            clickCount6++;
            if(clickCount6==5){
                speakPanel2.add(tbuttonBegin);
                speakPanel2.begin = 1;
            }
            if(clickCount6==29){
                cl.show(cards,"f");
                fp.requestFocus();
            }
            if(clickCount6==11||clickCount6==13||
                    clickCount6==15||clickCount6==17||
                    clickCount6==18||clickCount6==19||clickCount6==20||
                    clickCount6==24||clickCount6==25||clickCount6==26||
                    clickCount6==28||clickCount6==30){
                speakPanel2.changePic();
            }
            System.out.println(clickCount6);
            speakPanel2.changeFont();
        });
        buttonNext6.setBounds(1200,970,30,30);
        buttonNext6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });






        //plantPanel2.reSize(this);
        //sceneIntro
        JButton buttonGo = new JButton();
        introPanel.picPanel.setLayout(null);
        introPanel.picPanel.add(buttonGo);
        buttonGo.setContentAreaFilled(false);
        buttonGo.setBorder(null);
        buttonGo.setBounds(1700,800,238,84);
        buttonGo.setCursor(new Cursor(Cursor.HAND_CURSOR));
        buttonGo.addActionListener((e)->{
            choicePanel.af = 1;
            new MusicPlayer("/music/button.mp3").play();
            cl.show(cards,"scene1");
            panel.reSize(this);
            panel.requestFocus();
            new MusicPlayer("/music/button.mp3").play();
        });
        //scene过度
        JButton buttonGo2 = new JButton();
        introPanel2.picPanel.setLayout(null);
        introPanel2.picPanel.add(buttonGo2);
        buttonGo2.setContentAreaFilled(false);
        buttonGo2.setBorder(null);
        buttonGo2.setBounds(1700,800,238,84);
        buttonGo2.setCursor(new Cursor(Cursor.HAND_CURSOR));
        buttonGo2.addActionListener((e)->{
            cl.show(cards,"sp");
            speakPanel.reSize(this);
            speakPanel.requestFocus();
        });
        JButton buttonNext5 = new JButton();
        speakPanel.setLayout(null);
        speakPanel.add(buttonNext5);
        buttonNext5.setContentAreaFilled(false);
        buttonNext5.setBorder(null);
        buttonNext5.addActionListener(e -> {
            button.play();
            speakPanel.changeFont();

            clickCount5++;
            if(clickCount5==5||clickCount5==9||clickCount5==11||
                    clickCount5==13||clickCount5==14||clickCount5==15){
                speakPanel.changePic();
            }
            if(clickCount5==7){
                cl.show(cards,"scene5");
                snowPanel.reSize(this);
                snowPanel.requestFocus();
            }
            if(clickCount5==18){
                cl.show(cards,"sp2");
                snowPanel.requestFocus();

            }

            System.out.println(clickCount5);

        });
        buttonNext5.setBounds(1200,970,30,30);
        buttonNext5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });



        treePanel.setLayout(null);
        //tree 再来一把

        buttonAg.setContentAreaFilled(false);
        buttonAg.setBorder(null);
        buttonAg.setBounds(660,660,225,97);
        buttonAg.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });

        tbuttonBegin.setContentAreaFilled(false);
        tbuttonBegin.setBorder(null);
        tbuttonBegin.setBounds(660,880,225,97);
        tbuttonBegin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        tbuttonBegin.addActionListener((e)->{
            cl.show(cards,"scene6");
            treePanel.reSize(this);
            treePanel.begin = 1;
        });
        buttonAg.addActionListener((e)->{
            treePanel.remove(buttonAg);
            treePanel.again = 1;
        });
        //snowPanel



        JButton sbuttonBegin = new JButton();
        snowPanel.setLayout(null);
        snowPanel.add(sbuttonBegin);
        sbuttonBegin.setContentAreaFilled(false);
        sbuttonBegin.setBorder(null);
        sbuttonBegin.setBounds(860,980,225,97);
        sbuttonBegin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        sbuttonBegin.addActionListener((e)->{
            snowPanel.begin = 1;
            snowPanel.remove(sbuttonBegin);
            snowPanel.requestFocus();
        });






        //sceneBegin
        JButton buttonBegin = new JButton();
        beginPanel.add(buttonBegin);
        buttonBegin.setContentAreaFilled(false);
        buttonBegin.setBorder(null);
        beginPanel.setLayout(null);
        buttonBegin.setBounds(230,800,238,84);
        buttonBegin.setCursor(new Cursor(Cursor.HAND_CURSOR));
        buttonBegin.addActionListener((e)->{
            new MusicPlayer("/music/a1.m4a").play();
            beginPanel.musicPlayer.stop();
            cl.show(cards,"sceneIntro");
            introPanel.reSize(this);
        });




        //scene1
        panel.setLayout(null);
        JButton buttonNext = new JButton();
        panel.add(buttonNext);
        buttonNext.setContentAreaFilled(false);
        buttonNext.setBorder(null);
        buttonNext.addActionListener(e -> {

            new MusicPlayer("/music/button.mp3").play();
            clickCount++;
            if(clickCount<21) {
                panel.changeFont();
                flagScene1++;
                if (flagScene1 ==8|| flagScene1 == 9 || flagScene1 == 10 || flagScene1 == 13 || flagScene1 == 17) {
                    panel.changeImage();
                }
            }
            else{
                cl.show(cards,"choiceScene");
            }
            choicePanel.af++;

        });
        buttonNext.setBounds(1200,970,30,30);
        buttonNext.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });



        //choiceScene
        JButton buttonNext3 = new JButton();

        buttonNext3.setContentAreaFilled(false);
        buttonNext3.setBorder(null);
        buttonNext3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        buttonNext3.setBounds(1200,970,30,30);
        buttonNext3.addActionListener(e -> {
            button.play();
            clickCount4++;
            System.out.println(clickCount4);
            if(clickCount4>=9){
                cl.show(cards,"i2");
                introPanel2.reSize(this);
            }
            if(clickCount4==4||clickCount4==8){
                choicePanel.changeImg();
            }
            choicePanel.changeFont();
        });
        choicePanel.add(buttonNext3);
        choicePanel.setLayout(null);
        JButton buttonT = new JButton();
        buttonT.setBounds(50,100,580,750);
        choicePanel.add(buttonT);
        buttonT.setContentAreaFilled(false);
        buttonT.setBorder(null);
        buttonT.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new MusicPlayer("/music/button.mp3").play();
                plantPanel.pangbai = 1;
                cl.show(cards,"scene2");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                choicePanel.flag = 1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                choicePanel.flag = 0;
            }
        });

        JButton buttonS = new JButton();
        buttonS.setBounds(1200,700,90,70);
        choicePanel.add(buttonS);
        buttonS.setContentAreaFilled(false);
        buttonS.setBorder(null);
        buttonS.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                new MusicPlayer("/music/button.mp3").play();
                cl.show(cards,"ap2");
                animalPanel.requestFocus();
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                choicePanel.flag2 = 1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                choicePanel.flag2 = 0;
            }
        });
        //animalPanel2
        animalPanel.setLayout(null);
        JButton animalB = new JButton();


        animalPanel.add(animalB);
        win.setContentAreaFilled(false);
        win.setBorder(null);
        animalB.setContentAreaFilled(false);
        animalB.setBorder(null);
        win.setBounds(680,770,225,97);
        win.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        animalB.setBounds(700,830,225,97);
        animalB.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        win.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                animalPanel.remove(win);
                if(animalPanel.win==1)
                cl.show(cards,"ap2");
                if(animalPanel.lose==1){
                    animalPanel.w.x = 0;
                    animalPanel.cow.x = 700;
                    animalPanel.begin = 1;
                    animalPanel.end = 0;
                    animalPanel.flag1 = 0;
                    animalPanel.flag = 0;
                    animalPanel.flag2 = 0;
                    animalPanel.lose = 0;
                    animalPanel.win = 0;
                    animalPanel.score = 0;
                    animalPanel.w.speed = 1;
                    animalPanel.requestFocus();

                }
                animalPanel2.requestFocus();
            }
        });
        animalB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                animalPanel.begin = 1;
                animalPanel.requestFocus();
                animalPanel.remove(animalB);
                animalPanel2.wolf.x = 2000;
            }
        });
        animalPanel2.setLayout(null);
        JButton abuttonNext2 = new JButton();
        animalPanel2.add(abuttonNext2);
        abuttonNext2.setContentAreaFilled(false);
        abuttonNext2.setBorder(null);
        abuttonNext2.addActionListener(e -> {
            button.play();

            clickCount3++;
            System.out.println(clickCount3);
            if(clickCount3==6){
                animalPanel2.flg= 1;
            }
            if(clickCount3==10) {
                animalPanel2.resize(this);

                cl.show(cards,"animal");
                animalPanel.requestFocus();
                animalPanel.w.x = 0;
                animalPanel.w.y = 700;
                animalPanel.cow.x = 700;
                animalPanel.cow.y = 100;
            }
            if(clickCount3>=20)
            {
                animalPanel2.allEnd = 1;
                choicePanel.remove(buttonS);
                cl.show(cards,"choiceScene");
                return;
            }
            animalPanel2.changeFont();
        });
        abuttonNext2.setBounds(1200,970,30,30);
        abuttonNext2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });






        //scene2
        plantPanel.setLayout(null);
        JButton buttonNext2 = new JButton();
        plantPanel.add(buttonNext2);
        buttonNext2.setContentAreaFilled(false);
        buttonNext2.setBorder(null);

        buttonNext2.addActionListener(e -> {
            new MusicPlayer("/music/button.mp3").play();
            plantPanel.bf++;

            clickCount2++;
            System.out.println(clickCount2);
            if(clickCount2==10) {
                plantPanel2.reSize(this);
                cl.show(cards,"scene3");
            }
            if(clickCount2>=16)
            {
                choicePanel.remove(buttonT);
                plantPanel.allEnd = 1;
                choicePanel.add(buttonNext);
                cl.show(cards,"choiceScene");
                return;
            }
            plantPanel.changeFont();
            if(clickCount2==11||clickCount2==12){
                plantPanel.changePic();
            }


        });
        buttonNext2.setBounds(1200,970,30,30);
        buttonNext2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });




        //scene3
        plantPanel2.setLayout(null);
        JButton button3 = new JButton();


        plantPanel2.add(button3);
        button3.setBounds(900,1030,225,97);
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        button3.setContentAreaFilled(false);
        button3.setBorder(null);
        button3.addActionListener(e->{
            plantPanel2.flag = 1;
            plantPanel2.remove(button3);
            repaint();
        });
        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(plantPanel2.stopF>200&&plantPanel2.stopF<500){
                    cl.show(cards,"scene2");
                    plantPanel.resize(x);
                }
            }
        });
        timer.start();
        //tree

        haoye2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        });
        haoye2.setContentAreaFilled(false);
        haoye2.setBorder(null);
        haoye2.setBounds(660,660,225,97);
        haoye2.addActionListener((e)->{
            cl.show(cards,"sp2");
            speakPanel2.reSize();
            speakPanel2.begin = 0;
            speakPanel2.remove(tbuttonBegin);
            speakPanel2.requestFocus();
        });



    }

}


